<template>
  <div class="login-container">
    <div class="container">
      <!-- 右侧图片区域 start -->
      <div class="login-warp">
        <div class="login-warp-content">
          <div class="title">好物特工平台管理系统</div>
          <img src="../../assets/login_bg.png" />
        </div>
      </div>
      <!-- 右侧图片区域 end -->

      <div class="form-warp">
        <div class="form-warp-content">
          <div class="form-warp-text text-center">
            <h1 class="title">欢迎登录</h1>
            <p class="subtitle">好物入袭你我品质生活</p>
          </div>
          <el-form v-if="show" ref="loginForm" :model="form" :rules="rules" class="sign-form">
            <el-form-item prop="username">
              <el-input
                v-model="form.username"
                prefix-icon="el-icon-user"
                placeholder="请输入登录账号"
                size="medium"
              />
            </el-form-item>
            <el-form-item prop="password" class="login-password">
              <el-input
                ref="password"
                v-model="form.password"
                :type="passwordType"
                placeholder="请输入登录密码"
                size="medium"
                @keyup.enter.native="submit"
              />
              <span class="svg-container" @click="HideAndShowClick">
                <svg-icon :icon-class="this.passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>

              <div class="forget-password text-right mt-10 ft-12" @click="show = false">
                忘记密码？
              </div>
            </el-form-item>
            <el-form-item class="text-center">
              <el-button
                type="primary"
                style="background-color: #ff5f00"
                size="medium"
                :loading="loading"
                class="submit-btn"
                @click="submit"
              >
                立即登录
              </el-button>
            </el-form-item>
          </el-form>
          <div v-else class="login-password" style="margin-top: 60px">
            <div class="flex flex-col align-center">
              <img
                style="width: 170px; height: 170px"
                src="https://lease-oss.waka2020.com/lease/images/20211118/0f57946d4f4e48719487416e63578dac.jpg"
              />
              <div class="ft-14 mt-20">扫一扫，加管理员微信</div>
              <div
                class="text-right mt-20 ft-12"
                @click="show = true"
                style="width: 100%; color: #909399; cursor: pointer"
              >
                密码登录
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getlocationPosition } from '@/hooks'
export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入登陆账号',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入登陆密码',
            trigger: 'blur'
          }
        ]
      },
      show: true,
      loading: false,
      redirect: undefined,
      otherQuery: {},
      passwordType: 'password'
    }
  },
  computed: {},
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  methods: {
    // 获取参数
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    HideAndShowClick() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 获取二维码
    async getShoppingSet() {
      const { data } = await this.$http.platform.getShoppingSet()
      this.adminQrCode = data.adminQrCode
    },
    // 登录
    async submit() {
      const { ip, lat, lng } = await getlocationPosition()
      this.$refs.loginForm.validate(valid => {
        if (!valid) return false
        this.loading = true
        this.$store
          .dispatch('user/login', { ip, lat, lng, ...this.form })
          .then(() => {
            this.$router
              .push({ path: this.redirect || '/', query: this.otherQuery })
              .catch(() => {})
            // 获得上传policy和签名
            this.$store.dispatch('user/getPolicy')
            this.loading = false
          })
          .catch(({ msg, status }) => {
            this.$message.error(msg)
            if (status === 410) {
              this.$router.replace({
                path: '/410'
              })
            }
            this.loading = false
          })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  position: relative;
  display: flex;
  min-height: 100vh;
  .login-warp {
    display: flex;
    flex-direction: column;
    align-items: center;
    flex: 0 0 40%;
    background: #ff5f00;
    &-content {
      .title {
        font-size: 38px;
        margin-bottom: 60px;
      }
      display: flex;
      flex-direction: column;
      align-items: center;
      margin-top: 200px;
      color: #fff;
    }
    &-subtitle {
      font-weight: bold;
    }
    &-img {
      width: 120px;
      height: 70px;
    }
  }
}
.form-warp {
  display: flex;
  flex: 1;
  justify-content: center;
  .form-warp-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 240px;
    .sign-form {
      width: 320px;
      margin-top: 60px;
    }
  }
  &-text {
    .title {
      text-align: center;
      color: #5c5f68;
      margin: 0;
    }
    .subtitle {
      font-size: 14px;
      color: #d1d1d1;
      font-weight: bold;
      margin-top: 20px;
    }
  }
}
.el-button {
  width: 260px;
  background: #293c66;
  border: none;
  padding: 15px 0;
  border-radius: 15px;
  margin-top: 30px;
}
.forget-password {
  position: absolute;
  top: 30px;
  right: 0;
  color: $hw-text-color-title;
  cursor: pointer;
}
.login-password {
  position: relative;
}
.el-form-item {
  margin-bottom: 30px;
}
::v-deep .el-input__inner {
  height: 40px;
  line-height: 40px;
  border-radius: 15px;
}
.svg-container {
  position: absolute;
  height: 40px;
  line-height: 40px;
  right: 15px;
  top: 2px;
  text-align: centen;
  color: #9e9ea6;
  cursor: pointer;
}
</style>
